package com.yxhl.zoume.core.expresscar.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.car.fragment.common.CancelCarOrderFeedbackFragment;
import com.yxhl.zoume.core.expresscar.fragment.ExpressCarScheduleMapFragment;
import com.yxhl.zoume.core.specialcar.model.DialTelephoneEvent;
import com.yxhl.zoume.core.specialcar.model.OperationForToolbar;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.DeviceUtils;
import com.yxhl.zoume.utils.DiskCacheUtils;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.permission.PermissionHelper;

/**
 * Author：zhongshan   
 * Date：2016/12/08 18:30      
 * Description：专车－下单
 */
public class ExpressCarScheduleActivity extends BaseActivityWithToolBar {

    private static final String INTENT_KEY_EXPRESS_ORDER_SERVICE_ORDER_ID = "intent_key_express_order_service_order_id";
    private static final String FRAGMENT_EXPRESS_CAR_SCHEDULE_MAP = "ExpressCarScheduleMapFragment";
    private static final String FRAGMENT_CANCEL_CARORDER_FEEDBACK = "CancelCarOrderFeedbackFragment";
    //    private ExpressCarComponent mComponent;
    private String mOrderId;

    public static Intent getCallingIntent(Activity activity, String carOrderId) {
        Intent launch = new Intent(activity, ExpressCarScheduleActivity.class);
        launch.putExtra(INTENT_KEY_EXPRESS_ORDER_SERVICE_ORDER_ID, carOrderId);
        return launch;
    }

    /**
     * 推送专用
     */
    public static Intent getPushIntent(Context context, String carOrderId) {
        Intent launch = new Intent(context, ExpressCarScheduleActivity.class);
        launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launch.putExtra(INTENT_KEY_EXPRESS_ORDER_SERVICE_ORDER_ID, carOrderId);
        return launch;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIntentParams();
        initFragment();
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {
        if (view instanceof TextView) {
            TextView rightTv = (TextView) view;
            String operationStr = rightTv.getText().toString().trim();
            switch (operationStr) {
                case OperationForToolbar.CANCEL_TRIP:
                    switchCancleOrderFragment();
                    break;
                case OperationForToolbar.CUSTOMER_SERVICE:
                    if (PermissionHelper.checkPhonePermission(this))
                        DeviceUtils.dialTelPhone(this, getString(R.string.service_mobile));
                    else
                        PermissionHelper.takeCallPhonePermission(this, PermissionHelper.REQUEST_CODE_CALL_CUSTOMER_SERVICE);
                    break;
            }
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_express_car_schedule;
    }

    @Override
    protected void initializeInjector() {
//        mComponent = DaggerExpressCarComponent.show()
//                .appComponent(getAppComponent())
//                .activityModule(new ActivityModule(this))
//                .build();
    }

    private void getIntentParams() {
        Intent intent = getIntent();
        if (intent != null) {
            mOrderId = intent.getStringExtra(INTENT_KEY_EXPRESS_ORDER_SERVICE_ORDER_ID);
            DiskCacheUtils.getInstance(mBaseContext).saveExpressCarOrderId(mOrderId);
        }
    }

    private void initFragment() {
        addFragment(R.id.frame_express_car_service_body, ExpressCarScheduleMapFragment.newInstance(mOrderId), FRAGMENT_EXPRESS_CAR_SCHEDULE_MAP);
    }

    public void switchCancleOrderFragment() {
        replaceFragment(getSupportFragmentManager().findFragmentByTag(FRAGMENT_EXPRESS_CAR_SCHEDULE_MAP),
                CancelCarOrderFeedbackFragment.newInstance(mOrderId),
                R.id.frame_express_car_service_body, true,
                FRAGMENT_CANCEL_CARORDER_FEEDBACK);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            //打电话给司机
            case PermissionHelper.REQUEST_CODE_CALL_SPECIAL_CAR_DRIVER:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    RxBus.getInstance().send(new DialTelephoneEvent(DialTelephoneEvent.FROM_SCHEDULE_DRIVER_MOBILE));
                else
                    PromptUtils.showShort(this, getString(R.string.special_car_schedule_deny_hint));
                break;
            //打电话给客服
            case PermissionHelper.REQUEST_CODE_CALL_CUSTOMER_SERVICE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    DeviceUtils.dialTelPhone(this, getString(R.string.service_mobile));
                else
                    PromptUtils.showShort(this, getString(R.string.special_car_schedule_deny_hint));
                break;
        }
    }
}
